package com.example.demoes.controller;

import com.alibaba.fastjson2.JSON;
import com.example.demoes.dao.EtlAppESDao;
import com.example.demoes.entity.EtlApp;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/demo")
public class DemoController {
    @Autowired
    EtlAppESDao etlAppESMapper;
    @Autowired
    RestHighLevelClient highLevelClient;

    @GetMapping("/")
    public String demo(){
        EtlApp app = new EtlApp();
        app.setId(UUID.randomUUID().toString().replace("-",""));
        app.setCode("CCCC");
        app.setDes("描述信息");
        app.setEtlSys("sys");
        etlAppESMapper.save(app);
        log.info("-->{}",1111);
        return "HAHA";
    }
    @GetMapping("/query")
    public String query(String id){
        Optional<EtlApp> byId = etlAppESMapper.findById(id);
        if(byId == null) {
            return "无数据";
        }
        return byId.get().toString();
    }
    @GetMapping("/delete")
    public String delete(String id){
        etlAppESMapper.deleteById(id);
        return "OK";
    }
    @GetMapping("/querykey")
    public String querykey(String key,String id) throws IOException {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
                .trackScores(true).size(10).from(1).query(QueryBuilders.wildcardQuery("des",key))
                .sort("code", SortOrder.DESC);
        SearchRequest searchRequest = new SearchRequest()
                .source(searchSourceBuilder)
                .indices("notes");
        System.out.println("查询es入参："+JSON.toJSONString(searchRequest));
        SearchResponse searchResponse = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        // 遍历封装列表对象
        long total = searchResponse.getHits().getTotalHits().value;
        List<EtlApp> etlAppList = new ArrayList<>();
        SearchHit[] searchHits = searchResponse.getHits().getHits();
        for (SearchHit searchHit : searchHits) {
            etlAppList.add(JSON.parseObject(searchHit.getSourceAsString(), EtlApp.class));
        }
 /*
        IndexResponse response = highLevelClient.index(request, RequestOptions.DEFAULT);
       EtlApp app = new EtlApp();
        app.setId(id);
        app.setEtlSys(key);
        app.setDes(key);
        app.setName(key);
        app.setDescription(key);
        Page<EtlApp> etlApps = etlAppESMapper.searchSimilar(app, new String[]{"description", "des","name","etlSys"}, PageRequest.of(1, 3));
        if(etlApps == null) {
            return "无数据";
        }*/
        /*EtlApp app = new EtlApp();
        app.setId(id);
        etlAppESMapper.searchSimilar(app, new String[]{"id"}, PageRequest.of(1, 3));*/
        return etlAppList.toString();
    }
}
